﻿using AbstructFactory;
using ADManage.api.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XF.Common.WeChat;
using XF.Meeting.Api.Models;

namespace XF.Meeting.Api.sso
{
    public partial class wx_sso : System.Web.UI.Page
    {
        private readonly string apiurl = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["loginUrl"]));
        private readonly string secret = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["secret"]));
        private readonly string ExpTime = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ExpTime"]));
        private readonly string AppUrl = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["AppUrl"]));
        private readonly string AppKey = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["AppKey"]));
        private readonly string IdentityUrl = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["IdentityUrl"]));
        public AbsFactoryTE abs = (AbsFactoryTE)AbsFactory.CreateInterface();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                var code = Request.QueryString["code"];
                var mobile = GetUserInfo(code);
                var service = abs.CreateUserInfoInterface(mobile.userid, IPHelper.GetIPAddress);
            var result = service.Login(mobile.userid).Result;
            TokenInfo token1 = new TokenInfo();
            if (result != null)
            {

                var jwtHelper = new JWTHelper();
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("userName", result.Account);
                dic.Add("Name", result.AccountName);
                dic.Add("IsAdmin", result.IsAdmin);
                dic.Add("UserId", result.userId);
                dic.Add("role", result.IsAdmin == 3 ? "Admin" : "User");
                dic.Add("RegionalId", result.RegionalID);
                dic.Add("RegionName", result.RegionalName);
                dic.Add("email", result.email);
                dic.Add("OrgId", result.OrgId);
                    dic.Add("wxType", result.wxType);
                    var token = jwtHelper.Encode(dic, secret, Convert.ToInt32(ExpTime));
                Response.Redirect(apiurl + $"?token={token}&url=Hide_model", true);
            }
            else
            {
                //Response.Write("<script>alert('认证失败：无此用户，请联系管理员添加');window.history.go(-1);</script>");
                //Response.End();

            }
            }
        }
        private UserInfo GetUserInfo(string code)
        {
            var wx = new WeChatHelper();
            var userId = wx.GetUserId(code);
            return JsonConvert.DeserializeObject<UserInfo>(wx.GetUserInfo(userId));
        }
    }
}